package com.exam.role.dao.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.exam.base.dao.BaseDao;
import com.exam.role.dao.RoleDao;
import com.exam.role.model.Module;
import com.exam.role.model.Role;
import com.exam.role.model.RoleModule;
import com.exam.role.model.Student;
import com.exam.role.model.Teacher;
import com.exam.util.Pagetools;

public class RoleDaoImpl extends BaseDao<Module> implements RoleDao {

	public List<Module> findMenusByPage(Module module, Pagetools page) {
		// StringBuffer hql = new StringBuffer(
		// " select m from Module m,RoleModule rm,Role r where m.id=
		// rm.module.id and rm.role.id = r.id ");
		StringBuffer hql = new StringBuffer(
				" select m from Module m where m.flag= '1' ");
		if (!StringUtils.isEmpty(module.getTitle())) {
			hql.append(" and m.title like '%" + module.getTitle() + "%'");
		}
		if (!StringUtils.isEmpty(module.getLevel())) {
			hql.append(" and m.level = '" + module.getLevel() + "'");
		}
		return this.findByPage(hql.toString(), page);
	}

	@Override
	public List<Module> findSubMenus() {
		return this
				.findByHql(" select m from Module m where m.flag= '1' and m.level <>'2' order by m.level");
	}

	@Override
	public Module findMenuById(Integer id) {
		return this.get(id);
	}

	@Override
	public List<Role> findRolesByPage(Role role, Pagetools page) {
		StringBuffer hql = new StringBuffer(" select r from Role r where 1=1 ");
		if (!StringUtils.isEmpty(role.getName())) {
			hql.append(" and r.name like '%" + role.getName() + "%'");
		}
		return this.findByPage(hql.toString(), page);
	}

	@Override
	public void saveOrUpdate(Object obj) {
		this.save(obj);
	}

	@Override
	public List<Module> getMenuByRole(int roleId) {
		return this
				.findByHql(
						"select m from Module m,RoleModule rm,Role r where m.id=rm.module.id and rm.role.id = r.id and r.id=?",
						new Object[] { roleId });
	}

	@Override
	public List<Student> getStudentByRole(int roleId, Student student,
			Pagetools page) {
		StringBuffer hql = new StringBuffer(
				" select s from Student s where s.flag='1' and s.role.id=? ");
		if (!StringUtils.isEmpty(student.getName())) {
			hql.append(" and s.name like '%" + student.getName() + "%'");
		}
		if (!StringUtils.isEmpty(student.getUname())) {
			hql.append(" and s.uname like '%" + student.getUname() + "%'");
		}
		return this.findByPage(hql.toString(), new Object[] { roleId }, page);
	}

	@Override
	public List<Teacher> getTeacherByRole(int roleId, Teacher teacher,
			Pagetools page) {
		StringBuffer hql = new StringBuffer(
				" select t from Teacher t where t.flag='1' and t.role.id=? ");
		if (!StringUtils.isEmpty(teacher.getName())) {
			hql.append(" and t.name like '%" + teacher.getName() + "%'");
		}
		if (!StringUtils.isEmpty(teacher.getUname())) {
			hql.append(" and t.uname like '%" + teacher.getUname() + "%'");
		}
		return this.findByPage(hql.toString(), new Object[] { roleId }, page);
	}

	@Override
	public List<Module> getMenus() {
		return this.findByHql("select m from Module m where m.flag= '1'");
	}

	@Override
	public List<RoleModule> getRoleModulesByRole(int roleId) {
		return this.findByHql(
				"select rm from RoleModule rm where  rm.role.id = ?",
				new Object[] { roleId });
	}

	@Override
	public List<Student> findStudentsByPage(int roleId, Student student,
			Pagetools page) {
		StringBuffer hql = new StringBuffer(
				" select s from Student s where s.flag='1' ");
		if (!StringUtils.isEmpty(student.getName())) {
			hql.append(" and s.name like '%" + student.getName() + "%'");
		}
		if (!StringUtils.isEmpty(student.getUname())) {
			hql.append(" and s.uname like '%" + student.getUname() + "%'");
		}
		return this.findByPage(hql.toString(), new Object[] { roleId }, page);
	}

	@Override
	public List<Teacher> findTeachersByPage(int roleId, Teacher teacher,
			Pagetools page) {
		StringBuffer hql = new StringBuffer(
				" select t from Teacher t where t.flag='1'");
		if (!StringUtils.isEmpty(teacher.getName())) {
			hql.append(" and t.name like '%" + teacher.getName() + "%'");
		}
		if (!StringUtils.isEmpty(teacher.getUname())) {
			hql.append(" and t.uname like '%" + teacher.getUname() + "%'");
		}
		return this.findByPage(hql.toString(), new Object[] { roleId }, page);
	}
}
